angular.module('shopCartModule', [])
    .controller('shopCartCtr', ['$scope', 'httpFactory', '$filter','$location',function ($scope, httpFactory, $filter,$location) {
        var shopCartDatas = LsyStorage.getItem('shopCart');
        $scope.isEdit = false;
        $scope.newDatas = [];
        httpFactory.getData('./src/datas/proData.json')
            .then(function (data) {
                $scope.newDatas = $filter('getShopCartFilter')(data, shopCartDatas);
            });

        $scope.reduce = function (id) {
            var item = $filter('getItemById')($scope.newDatas, id);
            if (item && item.num > 1) {
                item.num--;
            }
        };
        $scope.isCheckAll = function () {
            var isAll = true;
            angular.forEach($scope.newDatas, function (item) {
                if (!item.checked) {
                    isAll = false;
                }
            });
            return isAll;
        };
        $scope.checkAll = function () {
            var isAll = true;
            angular.forEach($scope.newDatas, function (item) {
                if (!item.checked) {
                    isAll = false;
                }
            });
            angular.forEach($scope.newDatas, function (item) {
                item.checked = !isAll;
            });
        };
        $scope.totalPrice = function () {
            var total = 0;
            angular.forEach($scope.newDatas, function (item) {
                if (item.checked) {
                    total += item.num * item.price;
                }
            });
            return total;
        };
        /*删除*/
        $scope.remove = function (id) {
            var shopCartData = LsyStorage.getItem('shopCart');
            angular.forEach($scope.newDatas, function (item, key) {
                if (item.id == id) {
                    $scope.newDatas.splice(key, 1);
                    shopCartData.splice(key, 1);
                    LsyStorage.setItem('shopCart', shopCartData);
                }
            });
        };
        /*顶部数量改变*/
        $scope.dataNum = function () {
            if(($scope.newDatas).length){
                return ($scope.newDatas).length;
            }else {
                return 0;
            }
        };
        /*点击完成存储数量*/
        $scope.numClick=function(){
            $scope.isEdit=!$scope.isEdit;
            var shopCartData = LsyStorage.getItem('shopCart');
            for(var i=0;i<shopCartData.length;i++){
                if(shopCartData[i].id=$scope.newDatas[i].id){
                    shopCartData[i].num=$scope.newDatas[i].num
                }
            }
            LsyStorage.setItem('shopCart', shopCartData);
        };
        /*点击结算*/
        $scope.pay=function(){
            var confirmOrderData=[];
            angular.forEach($scope.newDatas, function (item) {
                if (item.checked == true) {
                    var orderObj={
                        id:item.id,
                        num:item.num
                    };
                    confirmOrderData.push(orderObj);
                }
            });
            LsyStorage.setItem('confirmOrderDatas', confirmOrderData);
            $location.path('confirm_order');
        }
    }]);
